<?php
namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Session;
use think\helper\Time;
use app\admin\model\Teacherentry as mTeacherentry;
use app\admin\model\Teacher;
use think\Config;

class Teacherentry extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = 'weiui';
    protected $model = null;
    protected $re_model = null;

    public function _initialize()
    {

        parent::_initialize();
        $this->model = new mTeacherentry();
        $this->re_model = new Teacher();
        //系统查无此人但是在白名单
        $this -> notInSysButWhite = Db::name('whitelist') -> column('tel');
    }

    public function index($ids=1)
    {
        
         if($this -> request -> isPost()){
            $params = $this->request->post("row/a");
            $params['create_time'] = time();
            $where['tel']= $params['tel'];
            $where['nickname'] = $params['nickname'];
            $error_msg = '请检查工号、姓名是否有误';
            $error_apply = '对不起，您尚未预约，请提前预约';


            if(!in_array($where['tel'],$this -> notInSysButWhite)){
                $per_row = $this->re_model->where($where)->find();
                if(!$per_row)
                {
                    $this->error($error_msg,null,0);
                }

                if($per_row -> inwhitelist != 1){
                    $entry_row = Db::name('entryapply') -> where([
                        'author_id' => $params['tel'],
                        'qstime'    => date('Y-m-d')
                    ])-> find();
                    if(!$entry_row)
                    {
                        $this->error($error_apply,null,0);
                    }
                }
            }
            $gateName = Config::get('gate')[$params['gate_id']];
            $params['gate_name'] = $gateName;

            $res = $this->model->allowField(true)->save($params);
            if ($res)
                $this->success('登记成功','',['nickname'=>$params['nickname'],'create_time'=>date("Y年m月d日 H时i分s秒",$params['create_time'])]);
            else
            {
                $this->error('登记失败');
                // $from_storage = 1;
            }
        }
        $this -> assign('ids',$ids);
        $title = '入校登记';
        $this -> assign('pageTitle',$title);
        return $this -> fetch();
    }
}